import requests
from lxml import etree
import csv
import time
import random

# https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=1&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=0&cvssscoremax=0.99&year=0&month=0&cweid=0&order=1&trc=876&sha=8cc1914b56963dbe370dba9383f2100c35cf3354

def findAll(num):
    cve_name = []
    page = str(num)
    url1 = 'https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=' + page + '&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=0&cvssscoremax=1.99&year=0&month=0&cweid=0&order=1&trc=1189&sha=2ee333910b0146938c917d67aa60489f458567df'
    url2 = 'https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=' + page + '&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=0&cvssscoremax=2.99&year=0&month=0&cweid=0&order=1&trc=8148&sha=309000079e74eb94dd19bab74ae6ee2280672c3b'
    url3 = 'https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=' + page + '&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=0&cvssscoremax=3.99&year=0&month=0&cweid=0&order=1&trc=9053&sha=a5f82bbf2e3b4c1a44673f277621d24e63e478cb'
    url4 = 'https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=' + page + '&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=0&cvssscoremax=4.99&year=0&month=0&cweid=0&order=1&trc=42080&sha=24a76f217bca516d9ddb84350e69616cb8f78973'
    url5 = 'https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=' + page + '&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=0&cvssscoremax=5.99&year=0&month=0&cweid=0&order=1&trc=33518&sha=487699e41035ffbb827ae68be83ace9ccd82c221'
    url6 = 'https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=' + page + '&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=6&cvssscoremax=6.99&year=0&month=0&cweid=0&order=1&trc=26425&sha=74e0bf73b5c24af1d8fa0497960c60660de4f638'
    url7 = 'https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=' + page + '&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=7&cvssscoremax=7.99&year=0&month=0&cweid=0&order=1&trc=35398&sha=8d1dce4336dc15b67abd26e84cfd7dee885ac426'
    url8 = 'https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=' + page + '&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=8&cvssscoremax=8.99&year=0&month=0&cweid=0&order=1&trc=878&sha=47211ec39e8a5bfc696c510450016af4d6c6f60d'
    url9 = 'https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=' + page + '&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=9&cvssscoremax=10&year=0&month=0&cweid=0&order=1&trc=19765&sha=c560d509f935c26128bfb13d2f2dadfcea62215b'
    url = 'https://www.cvedetails.com/vulnerability-list.php?vendor_id=0&product_id=0&version_id=0&page=' + page + '&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=0&cvssscoremax=0.99&year=0&month=0&cweid=0&order=1&trc=876&sha=8cc1914b56963dbe370dba9383f2100c35cf3354 '
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/57.0.2987.133 Safari/537.36 '
    }
    response = requests.get(url3, headers=headers)
    selector = etree.HTML(response.content.decode("utf-8"))
    # b = selector.xpath('//tr[contains(@class,"srrowns")]/td[2]')
    b = selector.xpath("//*[@class='srrowns']/td[2]//a")
    for item in b:
        cve_name.append(item.xpath('text()'))
    return cve_name
    # for item in b:
    #     td1 = item.xpath('td')[1]
    #     s1 = etree.tostring(td1).decode('utf-8')
    #     print(s1)


if __name__ == '__main__':
    sizes = [16,24,163,182,842,671,]
    size = 16
    size1 = 24
    size2 = 163
    size3 = 182
    size4 = 842
    size5 = 671
    size6 = 529
    size7 = 708
    size8 = 18
    size9 = 396
    for i in range(size3):
        print(i)
        name_list = findAll(i + 1)
        all = []
        for item in name_list:
            all.append(item[0])
        reader1 = csv.reader(all, quotechar="'")
        with open('cve/cve_names_3-1.csv', "a+", encoding='utf-8', newline='') as f:
            writer = csv.writer(f)
            writer.writerows(list(reader1))
        sleepTime = random.randint(5,18)
        print(sleepTime)
        time.sleep(sleepTime)

